package proiect.fis.UI;

import java.awt.BorderLayout;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;
import proiect.fis.commons.beans.Oras;

/**
 *
 * @author Boogie
 */
public class NewCityView extends javax.swing.JFrame {

    /**
     * Creates new form NewCityView
     */
    public interface NewCityViewListener{
        void onNewCityCreated(Oras o);
    }
    private CityView cityView;
    private List<NewCityViewListener> listeners;
    public NewCityView() {
        initComponents();
        cityView = new CityView();
        this.add(cityView, BorderLayout.CENTER);
        listeners = new LinkedList<>();
    }
    public void addNewCityViewListner(NewCityViewListener l){
        listeners.add(l);
    }
    public void removeNewCityViewListner(NewCityViewListener l){
        listeners.remove(l);
    }
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        buttonCancel = new javax.swing.JButton();
        buttonOk = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setResizable(false);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/new_pachage_logo.png"))); // NOI18N
        getContentPane().add(jLabel1, java.awt.BorderLayout.WEST);

        jPanel1.setPreferredSize(new java.awt.Dimension(656, 50));

        buttonCancel.setText("Cancel");
        buttonCancel.setToolTipText("");
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        buttonOk.setText("Ok");
        buttonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(510, Short.MAX_VALUE)
                .addComponent(buttonOk, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonCancel)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(16, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonCancel)
                    .addComponent(buttonOk))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_buttonCancelActionPerformed

    private void buttonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOkActionPerformed
        if(cityView.isValidInput()){
            Oras o = cityView.getOras();
            for (NewCityViewListener l : listeners) {
                l.onNewCityCreated(o);
            }
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(this, "Orasul exista deja", "Eroare", JOptionPane.ERROR_MESSAGE);
        }
        
    }//GEN-LAST:event_buttonOkActionPerformed

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonOk;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
